<template>
    <div></div>
</template>
<script setup>
import { ref, onMounted } from "vue";
import { tset_login, user_login } from "@/api/login";
import { search_hotel } from "@/api/searchHotel";
import { showLoadingToast } from "vant";

//授权
const login = () => {
    let appid = "wwdc0bc731d475dfb8";
    let redirect_uri = "https://wechatwork.ihg.com.cn/static/owner/index.html";
    let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${encodeURI(appid)}&redirect_uri=${encodeURI(
        redirect_uri
    )}&response_type=code&scope=snsapi_base#wechat_redirect`;
    window.location.replace(url);
};

//获取code
const getQueryVariable = (variable) => {
    const search = window.location.search || window.location.hash.split("?")[1] || "";
    const params = new URLSearchParams(search);
    return params.get(variable);
};

//测试登录
const testLogin = async (user_id) => {
    const res = await tset_login({ user_id: user_id });
    //存token
    localStorage.setItem("token", res.data);

    //获取权限
    getAuth();
};

//登录
const getLogin = async (code) => {
    let res = await user_login({
        code: code,
    });
    //存token
    localStorage.setItem("token", res.data);

    //获取权限
    getAuth();
};

/**
 * 获取权限
 *user_type  总管理员 1 | VPO 2 | OLT 3 | gm 4
 * 多个酒店 去酒店列表选择 单个酒店 直接进业主
 */
const getAuth = async () => {
    try {
        let res = await search_hotel({
            page: 1,
            pagesize: 10,
        });
        const { user_info, hotel_info } = res.data;
        if (hotel_info.data.length > 1) {
            window.location.replace("/static/owner/#/SearchHotel");
        } else {
            //存session
            sessionStorage.setItem("hotel_info", JSON.stringify(hotel_info.data[0]));
            window.location.replace("/static/owner/#/OwnerList");
        }
    } catch (error) {
        console.log(error);
    }
};

onMounted(() => {
    if (getQueryVariable("code")) {
        showLoadingToast({
            message: "加载中...",
            forbidClick: true,
        });
        //生产
        getLogin(getQueryVariable("code"));

        /**
         * 开发时使用 测试 http://localhost:5173/static/owner/#/Login?code=47686
         * 47686 总管理员
         */

        // testLogin(getQueryVariable("code"));
    } else {
        login();
    }
});
</script>
<style lang="scss" scoped></style>
